package com.ks.member.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ks.core.base.OperationContext;
import com.ks.core.constant.Const;
import com.ks.core.dto.SearchCriteriaBase;
import com.ks.core.exception.GunsException;
import com.ks.core.exception.WafKit;
import com.ks.core.util.ToolUtil;
import com.ks.member.controller.dto.Certification;
import com.ks.member.entity.MemberBase;
import com.ks.member.entity.MemberRealname;
import com.ks.member.dao.MemberRealNameMapper;
import com.ks.member.service.IMemberBaseService;
import com.ks.member.service.IMemberRealNameService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 会员-实名申请单 服务实现类
 * </p>
 *
 * @author kingdee123
 * @since 2020-12-21
 */
@Service
public class MemberRealNameServiceImpl extends ServiceImpl<MemberRealNameMapper, MemberRealname> implements IMemberRealNameService {
    @Resource
    private MemberRealNameMapper memberRealNameMapper;
    @Resource
    private IMemberBaseService memberBaseService;

    @Override
    public Map<String, Object> selectMemberRealName(OperationContext operationContext, SearchCriteriaBase searchCriteriaBase) {
        Map<String, Object> map = new HashMap<>();
        ArrayList<SearchCriteriaBase.matchField> fields = searchCriteriaBase.getFields();
        EntityWrapper<MemberRealname> wrapper = new EntityWrapper<>();
        MemberRealname memberRealname = new MemberRealname();
        memberRealname.setTenantID(operationContext.getTenantId().intValue());
        if (ToolUtil.isNotEmpty(fields)) {
            for (SearchCriteriaBase.matchField field : fields) {
                if ("realName".equals(field.getName())) {
                    memberRealname.setRealName(field.getValue());
                }
                if ("personID".equals(field.getName())) {
                    memberRealname.setPersonID(field.getValue());
                }
            }
        }
        ArrayList<SearchCriteriaBase.StateField> states = searchCriteriaBase.getStates();
        if (ToolUtil.isNotEmpty(states)) {
            for (SearchCriteriaBase.StateField state : states) {
                if ("checkResult".equals(state.getName())) {
                    if (ToolUtil.isNotEmpty(state.getValue())){
                        memberRealname.setCheckResult(Integer.valueOf(state.getValue()));
                    }
                }
            }
        }
        memberRealname.setDelMark(Const.DEL_FLAG_NORMAL);
        wrapper.setEntity(memberRealname);
        if (ToolUtil.isNotEmpty(searchCriteriaBase.getStartTime()) && ToolUtil.isNotEmpty(searchCriteriaBase.getEndTime())) {
            wrapper.and("DATE_FORMAT( createTime, '%Y-%m-%d' ) >=" + "'" + searchCriteriaBase.getStartTime() + "'");
            wrapper.and("DATE_FORMAT( createTime, '%Y-%m-%d' ) <=  " + "'" + searchCriteriaBase.getEndTime() + "'");
        }
        Integer integer = memberRealNameMapper.selectCount(wrapper);
        map.put("count", integer);
        RowBounds limit = new Page(searchCriteriaBase.getPageIndex(), searchCriteriaBase.getLimit());
        List<MemberRealname> memberRealnames = memberRealNameMapper.selectPage(limit, wrapper);
        map.put("memberRealnames", memberRealnames);
        return map;
    }

    @Override
    public Map<String, Object> selectAdminMemberRealName(OperationContext operationContext, SearchCriteriaBase searchCriteriaBase) {
        Map<String, Object> map = new HashMap<>();
        ArrayList<SearchCriteriaBase.matchField> fields = searchCriteriaBase.getFields();
        EntityWrapper<MemberRealname> wrapper = new EntityWrapper<>();
        MemberRealname memberRealname = new MemberRealname();
        if (ToolUtil.isNotEmpty(fields)) {
            for (SearchCriteriaBase.matchField field : fields) {
                if ("realName".equals(field.getName())) {
                    memberRealname.setRealName(field.getValue());
                }
                if ("personID".equals(field.getName())) {
                    memberRealname.setPersonID(field.getValue());
                }
                if ("tenantID".equals(field.getName())) {
                    memberRealname.setTenantID(Integer.valueOf(field.getValue()));
                }
            }
        }
        ArrayList<SearchCriteriaBase.StateField> states = searchCriteriaBase.getStates();
        if (ToolUtil.isNotEmpty(states)) {
            for (SearchCriteriaBase.StateField state : states) {
                if ("checkResult".equals(state.getName())) {
                    memberRealname.setCheckResult(Integer.valueOf(state.getValue()));
                }
            }
        }
        memberRealname.setDelMark(Const.DEL_FLAG_NORMAL);
        wrapper.setEntity(memberRealname);
        if (searchCriteriaBase.startTime != null && searchCriteriaBase.endTime != null) {
            wrapper.and("DATE_FORMAT( createTime, '%Y-%m-%d' ) >=" + "'" + searchCriteriaBase.getStartTime() + "'");
            wrapper.and("DATE_FORMAT( createTime, '%Y-%m-%d' ) <=  " + "'" + searchCriteriaBase.getEndTime() + "'");
        }
        Integer integer = memberRealNameMapper.selectCount(wrapper);
        map.put("count", integer);
        RowBounds limit = new Page(searchCriteriaBase.getPageIndex(), searchCriteriaBase.getLimit());
        List<MemberRealname> memberRealnames = memberRealNameMapper.selectPage(limit, wrapper);
        map.put("memberRealnames", memberRealnames);
        return map;
    }

    @Override
    public int commitCertification(OperationContext operationContext, MemberRealname memberRealname) {
        EntityWrapper<MemberRealname> wrapper = new EntityWrapper<>();
        MemberRealname realName = new MemberRealname();
        realName.setMemberID(memberRealname.getMemberID());
        realName.setTenantID(operationContext.getTenantId().intValue());
        realName.setMemberID(memberRealname.getMemberID());
        realName.setDelMark(Const.DEL_FLAG_NORMAL);
        wrapper.setEntity(realName);
        wrapper.and("checkResult in (0,1)");
        List<MemberRealname> memberRealnames = memberRealNameMapper.selectList(wrapper);
        if (ToolUtil.isNotEmpty(memberRealnames)) {
            throw new GunsException("用户已经提交过实名认证");
        }
        memberRealname.setTenantID(operationContext.getTenantId().intValue());
        memberRealname.setCreateUserID(operationContext.getUserId().intValue());
        Integer insert = memberRealNameMapper.insert(memberRealname);
        return insert;

    }

    @Override
    public int certificationAudit(OperationContext operationContext, int id, int state, String name, String remark) {
        MemberRealname memberRealname = memberRealNameMapper.selectById(id);
        if (ToolUtil.isNotEmpty(memberRealname)) {
            memberRealname.setCheckman(name);
            memberRealname.setCheckRemark(remark);
            memberRealname.setCheckTime(new Date());
            memberRealname.setCheckResult(state);
            Integer integer = memberRealNameMapper.updateById(memberRealname);
            if (state == 1) {
                MemberBase memberBase = memberBaseService.selectById(memberRealname.getMemberID());
                memberBase.setIRealName(memberRealname.getRealName());
                memberBase.setSAuthen(state);
                memberBaseService.updateById(memberBase);
            }
            return integer;
        }
        return 0;
    }

    public Map<String, Object> isResultMap(List list, SearchCriteriaBase searchCriteriaBase) {
        Map<String, Object> map = new HashMap<>();
        map.put("count", list.size());
        map.put("limit", searchCriteriaBase.getLimit());
        map.put("pageIndex", searchCriteriaBase.getPageIndex());
        map.put("hasMore", false);
        map.put("items", list);
        return map;
    }
}
